24 Lecture

CS201

Midterm & Final Term Short Notes

Memory Allocation

Memory allocation is the process of assigning a block of memory to a program for storing data during runtime. In C programming, memory allocation is done using functions such as malloc(), calloc(), and realloc(). Proper memory allocation is crit


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is memory allocation in C programming? a) Reserving memory for the program to store data during runtime b) Allocating memory for the program during compile time c) Storing data in memory during runtime d) None of the above

Answer: a) Reserving memory for the program to store data during runtime

  1. Which of the following is a function used for dynamic memory allocation in C? a) calloc() b) malloc() c) realloc() d) All of the above

Answer: d) All of the above

  1. What is the difference between malloc() and calloc() functions? a) malloc() allocates a block of memory of a specified size, while calloc() initializes the memory to 0 b) calloc() allocates a block of memory of a specified size, while malloc() initializes the memory to 0 c) malloc() and calloc() are the same function d) None of the above

Answer: a) malloc() allocates a block of memory of a specified size, while calloc() initializes the memory to 0

  1. What happens if malloc() or calloc() is unable to allocate the requested memory? a) The program crashes b) The function returns NULL c) The function returns a negative value d) None of the above

Answer: b) The function returns NULL

  1. What is a memory leak? a) When memory is not deallocated after it is no longer needed b) When memory is allocated but never used c) When memory is allocated and used but not freed after it is no longer needed d) None of the above

Answer: a) When memory is not deallocated after it is no longer needed

  1. Which function is used to free memory allocated by malloc(), calloc(), or realloc()? a) dealloc() b) free() c) remove() d) None of the above

Answer: b) free()

  1. What is stack memory allocation? a) Reserving memory for the program during runtime b) Allocating memory for the program during compile time c) Storing data in memory during runtime d) None of the above

Answer: b) Allocating memory for the program during compile time

  1. What is heap memory allocation? a) Reserving memory for the program during runtime b) Allocating memory for the program during compile time c) Storing data in memory during runtime d) None of the above

Answer: a) Reserving memory for the program during runtime

  1. What is the purpose of the realloc() function in C programming? a) To allocate a new block of memory b) To deallocate a block of memory c) To resize an existing block of memory d) None of the above

Answer: c) To resize an existing block of memory

  1. What is the potential risk of not properly managing memory allocation in C programming? a) Memory leaks b) Memory fragmentation c) Program crashes d) All of the above

Answer: d) All of the above



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is dynamic memory allocation? Answer: Dynamic memory allocation is the process of allocating memory to a program during runtime. In C programming, it is done using functions like malloc(), calloc(), and realloc().

  2. What is the difference between stack memory and heap memory? Answer: Stack memory is allocated during compile time, whereas heap memory is allocated during runtime. Stack memory is limited in size and is used for static memory allocation, while heap memory is used for dynamic memory allocation.

  3. What is the purpose of the malloc() function in C programming? Answer: The malloc() function is used to dynamically allocate memory to a program during runtime.

  4. What is a memory leak? Answer: A memory leak is a situation where memory that has been allocated is not properly deallocated, leading to the gradual depletion of available memory.

  5. What is the purpose of the calloc() function in C programming? Answer: The calloc() function is used to dynamically allocate memory to a program during runtime, and it initializes the memory to 0.

  6. What is the purpose of the free() function in C programming? Answer: The free() function is used to deallocate memory that has been allocated using malloc(), calloc(), or realloc().

  7. What is the potential danger of not properly deallocating memory in a program? Answer: The potential danger is that memory leaks can occur, causing the program to eventually run out of available memory.

  8. What is memory fragmentation? Answer: Memory fragmentation is a situation where the available memory becomes fragmented and is no longer contiguous, making it difficult to allocate large blocks of memory.

  9. What is the purpose of the realloc() function in C programming? Answer: The realloc() function is used to resize an existing block of memory that has been allocated using malloc(), calloc(), or realloc().

  10. What is the maximum amount of memory that can be allocated using malloc() in C programming? Answer: The maximum amount of memory that can be allocated using malloc() depends on the system and available memory, but it is typically limited to a few gigabytes.

Memory allocation is an important aspect of computer programming as it enables programs to use the available memory on a system efficiently. In C programming, memory allocation can be done in two ways: static memory allocation and dynamic memory allocation. Static memory allocation is done during compile time and is used to allocate memory for global and static variables. The amount of memory allocated is fixed and cannot be changed during runtime. Dynamic memory allocation, on the other hand, is done during runtime and is used to allocate memory to a program as needed. In C programming, this is done using functions like malloc(), calloc(), and realloc(). The malloc() function is used to allocate a block of memory of a specified size, while calloc() is used to allocate a block of memory and initialize it to 0. The realloc() function is used to resize an existing block of memory. It is important to note that when memory is allocated dynamically, it must be properly deallocated to prevent memory leaks. The free() function is used to deallocate memory that has been allocated using malloc(), calloc(), or realloc(). One potential issue with dynamic memory allocation is memory fragmentation, which occurs when available memory becomes fragmented and is no longer contiguous, making it difficult to allocate large blocks of memory. Memory allocation is an important topic in programming, especially in low-level languages like C. It requires a solid understanding of memory management and the various techniques for allocating and deallocating memory. Proper memory allocation is critical to writing efficient and bug-free code.